package com.qf.fmmall.service.impl;/*
 *徐韬
 *2022/1/23
 *12:54
 */

import com.qf.fmmall.LoginVO.resultVO;
import com.qf.fmmall.Mapper.userMapper;
import com.qf.fmmall.pojo.Users;
import com.qf.fmmall.service.userService;
import com.qf.fmmall.utils.md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceimpl implements userService {
    @Autowired
    private userMapper userMapper;

    @Override
    @Transactional
    public resultVO regist(String username, String password) {
        Users user = userMapper.selectUser(username);
        //可以注册
        if(user == null){
            String md5Pwd = md5Util.md5(password);
            Users users1 = new Users();
            users1.setUsername(username);
            users1.setPassword(md5Pwd);
            users1.setUserRegtime(new Date());
            users1.setUserImg("img/default.img");
            users1.setUserModtime(new Date());
            int i = userMapper.insertUser(users1);
            if(i>0){
                return new resultVO(10000,"注册成功",users1);
            }else {//注册失败
                return new resultVO(10001,"注册失败",null);
            }
        }else { //已存在
            return  new  resultVO(10001,"用户名已存在",null);
        }
    }

    @Override
    public resultVO login(String username, String password) {
        Users users = userMapper.selectUser(username);
        if(users == null){
            return new resultVO(10001,"用户名不存在",null);
        }else {
            String md5 = md5Util.md5(password);
            String password1 = users.getPassword();
            if(md5.equals(password1)){
                return new resultVO(10000,"登陆成功",users);
            }else {

                return new resultVO(10001,"密码错误",null);
            }


        }

    }
}
